package client

import (
	"context"
	"github.com/micro/go-micro/v2/client"
	"github.com/micro/go-micro/v2/client/grpc"

	"kop/framework/wrapper"
	"kop/modules/achievement/configuration/a_type"
	"kop/modules/quest/configuration/dailyquest/type"
	"kop/modules/quest/configuration/questtype"
	"kop/pb"
	"kop/pb/service"

	client2 "kop/modules/achievement/client"
	activityC "kop/modules/activity/client"
)

//Login       Type = 1  //
//RoleLevel   Type = 2  //
//VIP         Type = 3  //
//Income      Type = 4  //
//Power       Type = 5  //
//CaptainNum  Type = 6  //
//MaidNum     Type = 7  //
//ShipLevelUp Type = 8  //
//Employ      Type = 9  //
//Event       Type = 10 //
//StageNum    Type = 11 //
//Train       Type = 12 //
//Conquer     Type = 13 //
//Bar         Type = 14 //
//Date        Type = 15 //
//WorldBoss   Type = 16 //
//GuildBuild  Type = 17 //
//FinishNum   Type = 18 //

var QuestService service.QuestService

func init() {

	var c = grpc.NewClient(client.Wrap(wrapper.ClientLogWrapper))
	_ = c.Init()

	//s.Init(test.Flags()...)

	QuestService = service.NewQuestService("go.micro.srv.role", c)

}

func GetQuest(roleID int32) *pb.Quest {

	var quest, err = QuestService.GetQuest(context.Background(), &service.RoleRequest{RoleID: roleID})
	if err != nil {
		panic(err)
	}
	return quest
}

func Check(roleID int32, types ...questtype.QuestType) {

	var Types []int32
	for _, v := range types {
		Types = append(Types, int32(v))
	}

	_, _ = QuestService.Check(context.Background(), &service.QuestCheckRequest{RoleID: roleID, Types: Types})
}

//func Save(roleID int32, quest *pb.Quest) {
//
//	var resp, err = QuestService.Save(context.Background(), &service.QuestRequest{RoleID: roleID, Quest: quest})
//	if err != nil {
//		panic(err)
//	}
//	*quest = *resp
//}

// 主角爵位
func RoleLevel(roleID, level int32) {
	setNumber(roleID, questtype.RoleLevel, int64(level), 0)
	go client2.AddNumber(roleID, a_type.RoleLevel, 1)
}

func Ship(roleID, buildingID int32) {
	setNumber(roleID, questtype.Ship, 1, buildingID)
}

// 海盗船水手数量
func Sailor(roleID, sailor, buildingID int32) {
	setNumber(roleID, questtype.Sailor, int64(sailor), buildingID)
}

// 	指定船长达成指定等级
func CaptainLevel(roleID, captainID, level int32) {

	setNumber(roleID, questtype.CaptainLevel, int64(level), captainID)
}

// 每秒总收入
func Income(roleID int32, income int64) {
	setNumber(roleID, questtype.Income, income, 0)
	go client2.SetNumber(roleID, a_type.Income, income)
}

// 实力
func Power(roleID int32, power int64) {

	setNumber(roleID, questtype.Power, power, 0)
	go client2.SetNumber(roleID, a_type.Power, power)
	//var quest = GetQuest(roleID)
	//if questtype.QuestType(quest.Type) == questtype.Power {
	//	var captains = captainC.GetCaptains(roleID)
	//	var power int64 = 0
	//	for _, captain := range captains {
	//		power += captain.Power
	//	}
	//
	//	setNumber(roleID, questtype.Power, power, 0)
	//}
}

// 主线关卡
func Stage(roleID, stageID int32) {
	setNumber(roleID, questtype.Stage, 1, stageID)
	go client2.AddNumber(roleID, a_type.StageNum, 1)
}

// 点击海盗大厅
func Click(roleID int32, number int64) {
	addNumber(roleID, questtype.Click, number, 0)
}

// 海盗船升级
func ShipLevelUp(roleID int32) {
	addNumber(roleID, questtype.ShipLevelUp, 1, 0)
	go client2.AddNumber(roleID, a_type.ShipLevelUp, 1)
}

// 水手总数量
//func Employ(roleID, number int32) {
//	addNumber(roleID, questtype.Employ, int64(number), 0)
//	go client2.AddNumber(roleID, a_type.Employ, int64(number))
//}

// 船长数量等级
func CaptainLevelNum(roleID, n, level int32) {

	setNumber(roleID, questtype.CaptainLevelNum, int64(n), level)
}

// 船长爵位
func CaptainClass(roleID, n, class int32) {

	setNumber(roleID, questtype.CaptainClass, int64(n), class)
}

// 升级船长技能
func CaptainSkillLevelUp(roleID int32) {
	addNumber(roleID, questtype.CaptainSkillLevelUp, 1, 0)
}

// 升级侍女技能
func MaidSkillLevelUp(roleID int32) {
	addNumber(roleID, questtype.MaidSkillLevelUp, 1, 0)
}

// 登录次数
func Login(roleID int32) {
	addNumber(roleID, questtype.Login, 1, 0)
	go client2.AddNumber(roleID, a_type.Login, 1)

}

// 请酒次数
//func Bar(roleID int32, n int32) {
//	addNumber(roleID, questtype.Bar, int64(n), 0)
//	go client2.AddNumber(roleID, a_type.Bar, int64(n))
//}

// n: 约会次数, intimacy: 亲密度
func Date(roleID, serverID int32, n int64, intimacy int32) {

	//_, _ = QuestService.AddNumber(context.Background(), &service.QuestSetRequest{RoleID: roleID, Type: int32(questtype.Date), Number: n})
	_, _ = QuestService.DailyQuestAddNumber(context.Background(), &service.DailyQuestAddRequest{RoleID: roleID, Type: int32(_type.Date), Number: n})
	_, _ = activityC.ActivityService.AddPoints(context.Background(), &service.ActivityPointsRequest{RoleID: roleID, ServerID: serverID, Type: int32(activityC.Intimacy), Points: int64(intimacy)})
	client2.AddNumber(roleID, a_type.Date, 1)
}

// 训练次数
//func Train(roleID int32) {
//	addNumber(roleID, questtype.Train, 1, 0)
//	go client2.AddNumber(roleID, a_type.Train, 1)
//}

// 事件
func Event(roleID, serverID int32) {

	//_, _ = QuestService.AddNumber(context.Background(), &service.QuestSetRequest{RoleID: roleID, Type: int32(questtype.Event), Number: 1})
	_, _ = QuestService.DailyQuestAddNumber(context.Background(), &service.DailyQuestAddRequest{RoleID: roleID, Type: int32(_type.Event), Number: 1})
	_, _ = activityC.ActivityService.AddPoints(context.Background(), &service.ActivityPointsRequest{RoleID: roleID, ServerID: serverID, Type: int32(activityC.Event), Points: 1})
	client2.AddNumber(roleID, a_type.Event, 1)

}

// 征服
//func Conquer(roleID int32) {
//	client2.AddNumber(roleID, a_type.Conquer, 1)
//}

// 膜拜
func Worship(roleID int32) {

	_, _ = QuestService.AddNumber(context.Background(), &service.QuestSetRequest{RoleID: roleID, Type: int32(questtype.Worship), Number: 1})
	_, _ = QuestService.DailyQuestAddNumber(context.Background(), &service.DailyQuestAddRequest{RoleID: roleID, Type: int32(_type.Worship), Number: 1})
}

// 加入公会
func GuildJoin(roleID int32) {

	addNumber(roleID, questtype.GuildJoin, 1, 0)
}

// 公会建设
//func GuildBuild(roleID int32) {
//
//	addNumber(roleID, questtype.GuildBuild, 1, 0)
//	go client2.AddNumber(roleID, a_type.GuildBuild, 1)
//}

// 公会商店
func GuildShop(roleID int32) {

	addNumber(roleID, questtype.GuildShop, 1, 0)
}

// 海盗船船长数量
func ShipCaptainNum(building *pb.Building) {

	var n int64 = 0
	if building.Captain1 != 0 {
		n++
	}
	if building.Captain2 != 0 {
		n++
	}
	if building.Captain3 != 0 {
		n++
	}
	if building.Captain4 != 0 {
		n++
	}
	if building.Captain5 != 0 {
		n++
	}

	addNumber(building.RoleID, questtype.ShipCaptain, n, building.BuildingID)
}

func GetDailyQuests(roleID int32) []*pb.DailyQuest {

	var resp, err = QuestService.GetDailyQuests(context.Background(), &service.RoleRequest{RoleID: roleID})
	if err != nil {
		panic(err)
	}
	return resp.DailyQuests
}

func SaveDailyQuest(quest *pb.DailyQuest) {

	var resp, err = QuestService.SaveDailyQuest(context.Background(), quest)
	if err != nil {
		panic(err)
	}
	*quest = *resp
}

func DailyQuestAddNumber(roleID int32, typ _type.Type, number int64) {

	go func() {

		var req = service.DailyQuestAddRequest{
			RoleID: roleID,
			Type:   int32(typ),
			Number: number,
		}

		_, _ = QuestService.DailyQuestAddNumber(context.Background(), &req)
	}()
}

//Ring             Type = 14 // 每日获得任意魔戒
//FinishDailyQuest Type = 18 // 每日达成活跃度

// 月卡
func DailyMonthCard(roleID int32) {
	DailyQuestAddNumber(roleID, _type.MonthCard, 1)
}

// 年卡
func DailyYearCard(roleID int32) {
	DailyQuestAddNumber(roleID, _type.YearCard, 1)
}

// 日常点击
func DailyQuestClick(roleID int32, n int64) {
	DailyQuestAddNumber(roleID, _type.Click, n)
}

// 招募水手
func DailyQuestEmploy(roleID, n int32) {
	dailyQuestAddNumber(roleID, _type.Employ, int64(n))
	client2.AddNumber(roleID, a_type.Employ, int64(n))
}

// 船长升级
func DailyQuestCaptainLevelUp(roleID int32) {
	DailyQuestAddNumber(roleID, _type.CaptainLevelUp, 1)
}

// 训练次数
func DailyQuestTrain(roleID int32) {
	dailyQuestAddNumber(roleID, _type.Train, 1)
	client2.AddNumber(roleID, a_type.Train, 1)
}

//// 基地事件
//func DailyQuestEvent(roleID int32) {
//	DailyQuestAddNumber(roleID, _type.Event, 1)
//}

// 每日通关主线关卡
func DailyQuestStage(roleID int32) {
	DailyQuestAddNumber(roleID, _type.Stage, 1)
}

// 夺宝
func DailyQuestTreasure(roleID int32) {
	DailyQuestAddNumber(roleID, _type.Treasure, 1)
}

// 膜拜
func DailyQuestWorship(roleID int32) {
	DailyQuestAddNumber(roleID, _type.Worship, 1)
}

// 膜拜膜拜排行榜
func DailyQuestWorshipRanking(roleID int32) {
	DailyQuestAddNumber(roleID, _type.WorshipRanking, 1)
}

// 酒馆
func DailyQuestBar(roleID int32, n int32) {

	dailyQuestAddNumber(roleID, _type.Bar, int64(n))
	client2.AddNumber(roleID, a_type.Bar, int64(n))
}

// 每日获得任意魔戒
func DailyQuestRing(roleID int32, n int) {
	DailyQuestAddNumber(roleID, _type.Ring, int64(n))
}

// 世界Boss
func DailyQuestWorldBoss(roleID int32) {
	DailyQuestAddNumber(roleID, _type.WorldBoss, 1)
	go client2.AddNumber(roleID, a_type.WorldBoss, 1)
}

// 公会建设
func DailyGuildBuild(roleID int32) {

	dailyQuestAddNumber(roleID, _type.GuildBuild, 1)
	client2.AddNumber(roleID, a_type.GuildBuild, 1)
}

func setNumber(roleID int32, typ questtype.QuestType, number int64, ID int32) {

	go func() {

		_, _ = QuestService.SetNumber(context.Background(), &service.QuestSetRequest{RoleID: roleID, Type: int32(typ), Number: number, NeedID: ID})
	}()
}

func addNumber(roleID int32, typ questtype.QuestType, number int64, ID int32) {

	go func() {

		_, _ = QuestService.AddNumber(context.Background(), &service.QuestSetRequest{RoleID: roleID, Type: int32(typ), Number: number, NeedID: ID})
	}()
}

func dailyQuestAddNumber(roleID int32, typ _type.Type, number int64) {

	_, _ = QuestService.DailyQuestAddNumber(context.Background(), &service.DailyQuestAddRequest{RoleID: roleID, Type: int32(typ), Number: number})
}
